﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Xml.XPath;

namespace Timer
{
    public class XMLHelper
    {
        private XElement _doc = null;
        private readonly string _fileName;


        public XMLHelper()
        {
            // Open Config.xml
            this._fileName = string.Format("{0}\\Config.xml", AppDomain.CurrentDomain.BaseDirectory);
            this._doc = XElement.Load(this._fileName);
        }

        public IList<SectionEntity> ParseXmlForSection()
        {

            List<SectionEntity> list = new List<SectionEntity>();
            var sections = (from ele in this._doc.Descendants("Section")
                            select new SectionEntity
                            {
                                ID = int.Parse(ele.Attribute("ID").Value),
                                TotalTime = ele.Element("TotalTime").Value,
                                AlertTime = ele.Element("AlertTime").Value,
                                AlertSoundFile = ele.Element("AlertSoundFile").Value,
                                FinishSoundFile = ele.Element("FinishSoundFile").Value,
                                SectionTitle = ele.Element("Title").Value,
                                SectionName = ele.Attribute("Name").Value
                            }).ToList();



            return sections;
        }

        public IList<FontSizeEntity> ParseXmlForFontSize()
        {
            var fontSet = (from ele in this._doc.Descendants("FontSet")
                           select new FontSizeEntity
                           {
                               TimeFontSize = float.Parse(ele.Element("TimeFontSize").Value),
                               TitleFontSize = float.Parse(ele.Element("TitleFontSize").Value)
                           }).ToList();
            return fontSet;
        }

        public int Save(SectionEntity ent)
        {
            var section = (from ele in this._doc.Descendants("Section")
                          where ele.Attribute("ID").Value == ent.ID.ToString()
                          select ele).FirstOrDefault();

            section.Element("TotalTime").SetValue(ent.TotalTime);
            section.Element("AlertTime").SetValue(ent.AlertTime);
            section.Element("AlertSoundFile").SetValue(ent.AlertSoundFile);
            section.Element("FinishSoundFile").SetValue(ent.FinishSoundFile);
            section.Element("Title").SetValue(ent.SectionTitle);

            return this.Save();
        }

        public int Save(FontSizeEntity ent)
        {
            var font = (from ele in this._doc.Descendants("FontSet")
                        select ele).FirstOrDefault();

            font.Element("TitleFontSize").SetValue(ent.TitleFontSize);
            font.Element("TimeFontSize").SetValue(ent.TimeFontSize);

            return this.Save();
        }

        private int Save()
        {
            int result = 0;
            try
            {
                this._doc.Save(this._fileName);
                result = 1;
            }
            catch (Exception exobj)
            {
                result = -1;
            }

            return result;
        }
    }
}
